<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>登记盘点</span>
          </div>
          <div class="save_button">
            <div v-if="reviewS && isAdmin" class="buttonBox">
              <el-button type="success" icon="el-icon-check" size="mini" @click="reviewSuccess">盘点完成</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-form ref="form" :model="data" inline :rules="formRules">
          <el-input v-model="data.id" type="hidden" />
          <el-row :gutter="10">
            <el-col :xl="3" :lg="3" :md="5" :sm="6">
              <el-form-item prop="coding">
                <div>编号</div>
                <el-input v-model="data.coding" size="mini" :readonly="finished" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="3" :md="5" :sm="6">
              <el-form-item prop="name">
                <div>名称</div>
                <el-input v-model="data.name" size="mini" :readonly="finished" />
              </el-form-item>
            </el-col>
            <el-col :xl="2" :lg="3" :md="5" :sm="6">
              <el-form-item>
                <div>状态</div>
                <el-select v-model="data.status" size="mini" disabled>
                  <el-option v-for="(item, index) in partsTakeStatus" :key="index" :value="index" :label="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="3" :md="5" :sm="6">
              <el-form-item prop="store_id">
                <div>仓库</div>
                <v-select v-model="data.store_id" action="storeList" disabled />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="5" :sm="6">
              <el-form-item prop="admin_id">
                <div>盘点负责人</div>
                <v-select v-model="data.admin_id" disabled />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="5" :sm="6">
              <el-form-item prop="start_date">
                <div>盘点日期</div>
                <el-date-picker v-model="data.start_date" type="datetime" size="mini" placeholder="选择日期" align="right" value-format="yyyy-MM-dd HH:mm:ss" :readonly="finished" />
              </el-form-item>
            </el-col>
            <div v-if="data.verify_admin">
              <el-col :xl="4" :lg="4" :md="5" :sm="6">
                <el-form-item>
                  <div>验证人员</div>
                  <el-input v-model="data.verify_admin.name" disabled size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="4" :lg="4" :md="5" :sm="6">
                <el-form-item>
                  <div>验证时间</div>
                  <el-date-picker v-model="data.verify_date" type="date" size="mini" disabled />
                </el-form-item>
              </el-col>
            </div>
            <el-col :xl="4" :lg="4" :md="5" :sm="6">
              <el-form-item prop="sort">
                <div>排序</div>
                <el-input v-model="data.sort" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-tabs>
          <el-tab-pane label="详细">
            <el-table :data="list">
              <el-table-column prop="tools_true.name" label="工具" min-width="12%" />
              <el-table-column prop="count" label="系统数量" min-width="8%" />
              <el-table-column prop="number" label="实际数量" min-width="12%" />
              <el-table-column prop="create_time" label="时间" min-width="16%" />
              <el-table-column label="状态" min-width="28%">
                <template scope="scope">
                  <div v-if="scope.row.number === scope.row.count">
                    <span style="color: green">正常</span>
                  </div>
                  <div v-else-if="scope.row.number > scope.row.count">
                    <span style="color: red">盘多</span>
                  </div>
                  <div v-else-if="scope.row.number < scope.row.count">
                    <span style="color: red">盘少</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="" label="操作" min-width="8%">
                <template scope="scope">
                  <div v-if="reviewS">
                    <el-button type="text" size="small" @click="remove(scope.row.id)">删除</el-button>
                  </div>
                  <div v-else>-</div>
                </template>
              </el-table-column>
              <div slot="append" hidden>
                <el-form ref="tool_form" :model="order" :inline="true" class="hf_form append_other_admin" :rules="toolRules">
                  <el-row :gutter="10">
                    <el-col :xl="3" :lg="4" :md="5" :sm="6">
                      <el-form-item prop="tool_id">
                        <v-select v-model="order.tool_id" action="toolList" :query="{store_id:data.store_id}" />
                      </el-form-item>
                    </el-col>
                    <el-col :xl="3" :lg="4" :md="5" :sm="6">
                      <el-form-item>
                        <el-input :disabled="true" size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :xl="2" :lg="4" :md="5" :sm="6">
                      <el-form-item>
                        <el-input :disabled="true" size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :xl="3" :lg="4" :md="5" :sm="6">
                      <el-form-item prop="true_number">
                        <el-input v-model="order.true_number" size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :xl="4" :lg="4" :md="5" :sm="6">
                      <el-form-item>
                        <el-input :disabled="true" size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="7">
                      <el-form-item prop="remarks">
                        <el-input v-model="order.remarks" size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :xl="2" :lg="4" :md="5" :sm="6">
                      <el-form-item>
                        <el-button icon="el-icon-upload2" size="mini" @click="append" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="描述">
            <el-row :gutter="10">
              <el-col :xl="4" :lg="4" :md="5" :sm="6">
                <div>
                  <span>创建时间日期</span>
                  <el-input v-model="data.create_time" placeholder="创建时间日期" :disabled="true" class="mtb10" size="mini" />
                </div>
                <div>
                  <span>创建人</span>
                  <el-input v-model="data.cr_admin.name" placeholder="创建人" :disabled="true" class="mtb10" size="mini" />
                </div>
                <div v-if="data.update_time">
                  <div>
                    <span>最近更新</span>
                    <el-input v-model="data.update_time" placeholder="最近更新" :disabled="true" class="mtb10" size="mini" />
                  </div>
                  <div>
                    <span>更新者</span>
                    <el-input v-model="data.up_admin.name" placeholder="更新者" :disabled="true" class="mtb10" size="mini" />
                  </div>
                </div>
              </el-col>
              <el-col :span="20">
                <wangeditor id="content" v-model="data.content" />
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-row>
    </div>
  </div>
</template>

<script>
import API from '@/api/tool/stockTake'
import { partsTakeStatus } from '@/utils/options'
import VSelect from '@/components/select'
import Wangeditor from '@/components/editor/wangeditor'
import { mapGetters } from 'vuex'
export default {
  inject: ['backUrl', 'delOne', 'update', 'appendParts', 'removeParts', 'goUrl'],
  components: {
    VSelect,
    Wangeditor
  },
  data() {
    return {
      data: {
        'cr_admin': {
          'name': ''
        }
      },
      partsTakeStatus,
      finished: false,
      reviewS: false,
      isAdmin: false,
      list: [],
      order: {},
      parts: {},
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入盘点单名称',
            trigger: 'blur'
          }
        ],
        store_id: [
          {
            required: true,
            message: '请选择仓库',
            trigger: 'change'
          }
        ],
        admin_id: [
          {
            required: true,
            message: '请选择盘点负责人',
            trigger: 'change'
          }
        ],
        start_date: [
          {
            required: true,
            message: '请输入盘点日期',
            trigger: 'blur'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      toolRules: {
        tool_id: [
          {
            required: true,
            message: '请选择工具',
            trigger: 'change'
          }
        ],
        true_number: [
          {
            required: true,
            message: '请输入实际数量',
            trigger: 'blur'
          }
        ],
        remarks: [
          {
            required: true,
            message: '请输入备注',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'roles',
      'userId'
    ])
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.data.error_code === 89404) {
        this.$alert('您请求的盘点单不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      this.data = response.data
      this.list = response.data.tool
      if (response.data.status === 0) {
        this.reviewS = true
      }
      const thisRole = this.roles[0]
      if (thisRole === 16 || thisRole === 116) {
        this.isAdmin = true
      }
      if (response.data.status === 1) {
        this.finished = true
      }
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    append() {
      const thisForm = this.$refs.tool_form
      const tool_data = {
        'tool_id': this.order.tool_id,
        'remarks': this.order.remarks,
        'true_number': this.order.true_number,
        'store': this.$route.params.id
      }
      const errorCode = 89004
      const errorMessage = '该工具已经存在于此盘点单'
      this.appendParts(API, thisForm, tool_data, errorCode, errorMessage)
    },
    remove(id) {
      const tool_data = {
        'id': id
      }
      this.removeParts(API, id, tool_data)
    },
    reviewSuccess() {
      const reviewData = {
        'status': 1,
        'id': this.data.id
      }
      API.review(reviewData).then(response => {
        if (response.data.error_code === 1) {
          this.$message.success('盘点完成！')
          this.backUrl()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    goBack() {
      this.goUrl('/tool/take')
    }
  }
}
</script>
